<?php


/** ensure this file is being included by a parent file */
defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );

/**
 * @package	
 */
class HelpDeskControllerTickets extends HelpDeskController 
{
	/**
	 * constructor
	 */	
	function __construct() 
	{
		parent::__construct();
		
		// register tasks
		$this->registerTask( 'list', 'display' );
		$this->registerTask( 'selectuser', 'add' );
		$this->registerTask( 'new', 'edit' );
		$this->registerTask( 'remove', 'delete' );
		$this->registerTask( 'setfeedback', 'changestatus' );
		$this->registerTask( 'setclosed', 'changestatus' );
		$this->registerTask( 'setpending', 'changestatus' );
		
	}
	
	/**
	 * display 
	 * @return void
	 */
	function display() 
	{
		HelpDeskRequest::setVar( 'view', 'tickets' );
		HelpDeskRequest::setVar( 'layout', 'default'  );
		parent::display();
	}
	
	/**
	 * display the ticket
	 * @return void
	 */
	function view() 
	{
		HelpDeskRequest::setVar( 'view', 'tickets' );
		HelpDeskRequest::setVar( 'layout', 'view'  );
		parent::display();
	}
	
	/**
	 * display the edit form
	 * @return void
	 */
	function edit() 
	{
		HelpDeskRequest::setVar( 'view', 'tickets' );
		HelpDeskRequest::setVar( 'layout', 'form' );
		parent::display();
	}
	
	/*
	 * display the new ticket form
	 */
	function add()
	{
		$this->setRedirect('index2.php?option=com_helpdesk&controller=users&task=selectuser');
	}
	
	/**
	 * 
	 * @return unknown_type
	 */
	function save() 
	{
		$model 	= &$this->getModel( 'tickets' );
		$user 	= HelpDeskConfig::getUser();
		
		$msg = new stdClass();
		$msg->type 		= '';
		$msg->message 	= '';
				
		if ( $data = $model->save() ) 
		{
			$msg->type 		= 'message';
			$msg->message  .= HelpDeskText::_( 'Saved' );

			// fire plugins
			$dispatcher = HelpDeskHelperPlugin::getInstance();
			$dispatcher->trigger( 'onAfterSaveSuccess', array( $data, $user, $msg ) );

		} else {
			// save failed
			$msg->type = 'notice';			
			$msg->message = HelpDeskText::_( 'Save Failed' )." - ".$model->getError();
		}

		$link = 'index2.php?option='._THISCOMPONENT.'&controller=tickets&task=list';
		$this->setRedirect( $link, $msg->message, $msg->type );
		return;
	}
	
	/*
	 * remove record(s)
	 */
	function delete()
	{
		$model = $this->getModel('tickets');
		if (!$model->delete())
		{
			$msg = HelpDeskText::_('Error: One or More Ticket Could not be Deleted');
		}
		else
		{
			$msg = HelpDeskText::_('Tickets Deleted');
		}

		$link = 'index2.php?option='._THISCOMPONENT.'&controller=tickets&task=list';		
		$this->setRedirect( $link, $msg );
	}
	
	/**
	 * addcomment
	 * @return void
	 */
	function addcomment() 
	{
		global $mainframe;

		$model = &$this->getModel( 'tickets' );
		$user 	= HelpDeskConfig::getUser();
		
		$msg = new stdClass();
		$msg->type 		= '';
		$msg->message 	= '';
				
		if ( $data = $model->addcomment() ) 
		{
			$msg->type 		= 'message';
			$msg->message  .= HelpDeskText::_( 'New Comment Saved' );

			// fire plugins
			$dispatcher = HelpDeskHelperPlugin::getInstance();
			$dispatcher->trigger( 'onAfterAddCommentSuccess', array( $data, $user, $msg ) );

		} else {
			// save failed
			$msg->type = 'notice';			
			$msg->message = HelpDeskText::_( 'New Comment Failed' )." - ".$model->getError();
		}

		$surrounding = $model->getSurrounding( $data->id );
		$prev = intval( $surrounding["prev"] );
		$next = intval( $surrounding["next"] );
		
		$link = 'index2.php?option='._THISCOMPONENT.'&controller=tickets&task=view&cid[]='.$data->id."&prev={$prev}&next={$next}";
		
		$this->setRedirect( $link, $msg->message, $msg->type );	
	}

	/**
	 * change published value
	 * @return void
	 */
	function followup() 
	{
		$model = $this->getModel( 'tickets' );

		$msg = new stdClass();
		$msg->type 		= '';
		$msg->message 	= '';
		
		if ( $data = $model->followup() ) {
			$msg->type 		= 'message';
			$msg->message  .= HelpDeskText::_( 'Updated' );

			// fire plugins
			$dispatcher = HelpDeskHelperPlugin::getInstance();
			$dispatcher->trigger( 'onAfterMarkFollowup', array( $data, $user, &$msg ) );

		}
		else {
			$msg->type = 'notice';			
			$msg->message = HelpDeskText::_( 'Update Failed' )." - ".$model->getError();
			
		}
		
		$link = 'index2.php?option='._THISCOMPONENT.'&controller=tickets&task=list';		
		$this->setRedirect( $link, $msg->message, $msg->type );
	}

	/**
	 * download the file
	 * @return void
	 */
	function downloadfile() 
	{
		HelpDeskRequest::setVar( 'view', 'ticket' );
		HelpDeskRequest::setVar( 'layout', 'default' );
		$id	= intval ( HelpDeskRequest::getVar( "id" ) );

		$model 	= $this->getModel( 'tickets' );
		$row	= $model->getTable( 'ticket' );
		$row->load( $id );
		$user 	= HelpDeskConfig::getUser();
		$fileid = intval( HelpDeskRequest::getVar( 'fileid' ) );
		
		// cannot view
		if ( !$canView = HelpDeskHelperHelpDesk::canViewAttachment( $fileid, $row->id, $user->id ) ) {
			$link = 'index2.php?option='._THISCOMPONENT.'&controller=tickets&task=list';
			$link = HelpDeskRoute::_( $link, false );
			$msg = HelpDeskText::_( 'Invalid Ticket' );
			$this->setRedirect( $link, $msg, 'notice' );
        	return false;
		}
		
		$file = $model->getTable( 'file' );
		$file->load( (int) $fileid );
	
		if ($viewAttachment = HelpDeskHelperHelpDesk::viewAttachment( $file )) {
			$link = 'index2.php?option='._THISCOMPONENT.'&controller=tickets&task=view&cid[]='.$row->id;
			$link = HelpDeskRoute::_( $link, false );
			$this->setRedirect( $link );
		}
		// parent::display();
	}

	/**
	 * addfile
	 * @return void
	 */
	function addfile() 
	{
		global $mainframe;
		
		$model = $this->getModel( 'tickets' );
		$user = HelpDeskConfig::getUser();
		
		$msg = new stdClass();
		$msg->type 		= '';
		$msg->message 	= '';
				
		if ( $data = $model->addfile() ) {

			$msg->type 		= 'message';
			$msg->message  .= HelpDeskText::_( 'New File Saved' );

			// fire plugins
			$dispatcher = HelpDeskHelperPlugin::getInstance();
			$dispatcher->trigger( 'onAfterAddFileSuccess', array( $data, $user, &$msg ) );

		} else {
			// save failed
			$msg->type = 'notice';			
			$msg->message = HelpDeskText::_( 'New File Failed' )." - ".$model->getError();
		}

		$link = 'index2.php?option='._THISCOMPONENT.'&controller=tickets&task=view&cid[]='.$data->id;
		
		$this->setRedirect( $link, $msg->message, $msg->type );	
	}

	/**
	 * change ticket category
	 * @return void
	 */
	function moveticket() {
		global $mainframe;

		$model = $this->getModel( 'tickets' );
		$user = HelpDeskConfig::getUser();
		
		$msg = new stdClass();
		$msg->type 		= '';
		$msg->message 	= '';
		$msg->link 		= 'index2.php?option='._THISCOMPONENT.'&controller=tickets&task=list';
		
		if ( $data = $model->moveticket() ) {

			$msg->type 		= 'message';
			$msg->message  .= HelpDeskText::_( 'Category Change Success' );

			// fire plugins
			$dispatcher = HelpDeskHelperPlugin::getInstance();
			$dispatcher->trigger( 'onAfterMoveTicketSuccess', array( $data, $user, $msg ) );
			
			$surrounding = $model->getSurrounding( $data->id );
			$prev = intval( $surrounding["prev"] );
			$next = intval( $surrounding["next"] );
			
			$msg->link = 'index2.php?option='._THISCOMPONENT.'&controller=tickets&task=view&cid[]='.$data->id."&prev={$prev}&next={$next}";
				

		} else {
			// save failed
			$msg->type = 'notice';			
			$msg->message = HelpDeskText::_( 'Category Change Failed' )." - ".$model->getError();
			
			
		}

		
		$this->setRedirect( $msg->link, $msg->message, $msg->type );	
	}

	/**
	 * save a record (and redirect to main page)
	 * @return void
	 */
	function changestatus() {
		global $mainframe;

		$model = $this->getModel( 'tickets' );
		$user = HelpDeskConfig::getUser();
		
		$msg = new stdClass();
		$msg->type 		= '';
		$msg->message 	= '';
		$msg->link 		= 'index2.php?option='._THISCOMPONENT.'&controller=tickets&task=list';
			
		if ( $data = $model->changestatus() ) 
		{

			$msg->type 		= 'message';
			$msg->message  .= HelpDeskText::_( 'Status Change Success' );

			// fire plugins
			$dispatcher = HelpDeskHelperPlugin::getInstance();
			$dispatcher->trigger( 'onAfterChangeStatusSuccess', array( $data, $user, &$msg ) );
			
			$surrounding = $model->getSurrounding( $data->id );
			$prev = intval( $surrounding["prev"] );
			$next = intval( $surrounding["next"] );
			$msg->link = 'index2.php?option='._THISCOMPONENT.'&controller=tickets&task=view&cid[]='.$data->id."&prev={$prev}&next={$next}";
			
		} else {
			// save failed
			$msg->type = 'notice';			
			$msg->message = HelpDeskText::_( 'Status Change Failed' )." - ".$model->getError();
		}
				
		$this->setRedirect( $msg->link, $msg->message, $msg->type );	
		
	}
	
	
}